//
//  MyPageViewController.m
//  MyPageViewController
//
//  Created by 蔡成汉 on 15/5/26.
//  Copyright (c) 2015年 JW. All rights reserved.
//

#import "MyPageViewController.h"
#import "MyPageItemViewController.h"

@interface MyPageViewController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate>
{
    NSArray *dataArray;
}
@end

@implementation MyPageViewController

/**
 *  重写init方法，获取数据源数据
 *
 *  @param array 数据数组
 *
 *  @return MyPageViewController
 */
-(id)initWithArray:(NSArray *)array
{
    return [self initWithArray:array index:0 transitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
}

/**
 *  重写init方法，获取数据源
 *
 *  @param array 数据数组
 *  @param index 起始页索引，默认为0
 *
 *  @return MyPageViewController
 */
-(id)initWithArray:(NSArray *)array index:(NSInteger)index
{
    return [self initWithArray:array index:index transitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
}

/**
 *  重写init方法，获取数据源
 *
 *  @param array                 数据数组
 *  @param index                 起始页索引，默认为0
 *  @param style                 翻页风格
 *  @param navigationOrientation 左右翻页还是上下翻页
 *  @param options               可扩充数据
 *
 *  @return MyPageViewController
 */
-(id)initWithArray:(NSArray *)array index:(NSInteger)index transitionStyle:(UIPageViewControllerTransitionStyle)style navigationOrientation:(UIPageViewControllerNavigationOrientation)navigationOrientation options:(NSDictionary *)options
{
    self = [super initWithTransitionStyle:style navigationOrientation:navigationOrientation options:options];
    if (self)
    {
        if ([[UIDevice currentDevice] systemVersion].floatValue>=7.0)
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        dataArray = array;
        self.dataSource = self;
        self.delegate = self;
        self.view.backgroundColor = [UIColor whiteColor];
        
        NSArray *controllers = [NSArray arrayWithObjects:[self viewControllerAtIndex:index], nil];
        [self setViewControllers:controllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
}

#pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSLog(@"后退");
    NSUInteger index = [self indexOfViewController:(MyPageItemViewController *)viewController];
    if ((index == 0) || (index == NSNotFound))
    {
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    NSLog(@"前进");
    NSUInteger index = [self indexOfViewController:(MyPageItemViewController *)viewController];
    if (index == NSNotFound)
    {
        return nil;
    }
    index++;
    if (index == [dataArray count])
    {
        return nil;
    }
    return [self viewControllerAtIndex:index];
}


#pragma mark - UIPageViewControllerDelegate

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray *)pendingViewControllers
{
    
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    
}

- (UIPageViewControllerSpineLocation)pageViewController:(UIPageViewController *)pageViewController spineLocationForInterfaceOrientation:(UIInterfaceOrientation)orientation
{
    // Set the spine position to "min" and the page view controller's view controllers array to contain just one view controller. Setting the spine position to 'UIPageViewControllerSpineLocationMid' in landscape orientation sets the doubleSided property to YES, so set it to NO here.
    UIViewController *currentViewController = self.viewControllers[0];
    NSArray *viewControllers = @[currentViewController];
    [self setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    
    self.doubleSided = NO;
    return UIPageViewControllerSpineLocationMin;
}

- (MyPageItemViewController *)viewControllerAtIndex:(NSUInteger)index
{
    // Return the data view controller for the given index.
    if (([dataArray count] == 0) || (index >= [dataArray count]))
    {
        return nil;
    }
    
    // Create a new view controller and pass suitable data.
    MyPageItemViewController *pageItemViewController = [[MyPageItemViewController alloc]init];
    pageItemViewController.imageName = [dataArray objectAtIndex:index];
    return pageItemViewController;
}

- (NSUInteger)indexOfViewController:(MyPageItemViewController *)viewController
{
    // Return the index of the given data view controller.
    // For simplicity, this implementation uses a static array of model objects and the view controller stores the model object; you can therefore use the model object to identify the index.
    return [dataArray indexOfObject:viewController.imageName];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
